using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace MusicEvolution.Database
{
	public class DataBaseTable<U,V> where U: DataBaseRow,new() where V: new()
	{        
		protected string _tableName;
        protected string _select;
        protected string _update;

        public DataBaseTable()
        {
            _select = "select * from " + _tableName + " ";
        }

        /*protected virtual DataFieldList Get(long ID)
        {
            get { return this.GetByID(ID); }            
        }*/

		public string Tablename
		{
			get
			{
                return _tableName;
			}
		}

        protected void Update(DataBaseRow Values)
        {
            DatabaseBackend.Backend.UpdateRow(Values.Fields, Values.ID, _tableName);            
        }

        protected List<DataFieldList> Read(string SQLCommand)
        {
            return DatabaseBackend.Backend.ExecuteQuery(SQLCommand);
        }

        protected int Delete(long ID)
        {
            return DatabaseBackend.Backend.ExecuteNonQuery("Delete from " + _tableName + " where ID=" + ID);
        }

		protected void Insert(DataBaseRow row)
		{
            DatabaseBackend.Backend.AddRow(row, _tableName);
		}

		protected DataFieldList GetByID(long ID)
		{
            List<DataFieldList> drs = DatabaseBackend.Backend.ExecuteQuery("Select * from " + _tableName + " where ID=" + ID.ToString());
            if (drs.Count == 0)
            {
                return null;
            }
            return drs[0];
		}

        protected List<DataFieldList> GetByValues(List<DataField> List)
        {
            string values = " where ";
            foreach (DataField field in List)
            {
                values += field.Name + "=\"" + field.Value + "\" and ";
            }
            values = values.Remove(values.Length - 5);
            return DatabaseBackend.Backend.ExecuteQuery(_select + values);
        }
	
        protected static V _instance;

        public static V Instance
        {
            get
            {
                if (_instance == null) _instance = new V();
                return _instance;
            }
        }

        

        public U this[long ID]
        {
            get
            {
                U aa = new U();
                aa.Load(GetByID(ID));
                return aa;
            }
        }

        public virtual U Add(List<DataField> list)
        {
            //DataField field1 = new DataField("Playlist", PlayList.ToString());
            //DataField field2 = new DataField("Track", Track.ToString());
            //List<DataFieldList> result = base.GetByValues(new List<DataField>() { field1, field2 });
            U a = new U();
            
            a.Load(list);
            Insert(a);
            a.Load(GetByValues(list)[0]);

            return a;
        }

        public void Update(U Value)
        {
            Update(Value);
        }

        public void Remove(long ID)
        {
            Delete(ID);
        }

        public void Remove(U Value)
        {
            Delete(Value.ID);
        }
    
    }
}
